package org.example.c_0_001;

import org.junit.Test;

/**
 * 默认情况当占用锁的线程抛出异常后，锁会被释放。
 */
public class T_Exception_Syncronized {
    private int count = 0;
    public class ARunnable implements Runnable {
        @Override
        public void run() {
            m();
        }
    }

    // 锁对象this
    synchronized void m(){
        while (true){
            System.out.println(Thread.currentThread().getName()+":"+count);
            count++;
            if (count == 5){
                try { // 重点* ： 抛出的异常如果手动行了处理则不会释放锁。
                    int n = count / 0;
                }catch (ArithmeticException e){

                }
            }
            if (count == 10){
                count = 0;
                return;
            }
        }
    }

    @Test
    public void run(){
        Thread thread = new Thread(new ARunnable(),"t1");
        Thread thread1 = new Thread(new ARunnable(),"t2");
        thread.start();
        thread1.start();

        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
